package com.jdym.back.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jdym.R;
import com.jdym.back.entity.Firm;
import com.jdym.back.entity.Message;
import com.jdym.back.entity.vo.LoanInfo;
import com.jdym.back.entity.vo.MessageManage;
import com.jdym.back.service.LoanService;
import com.jdym.back.service.MessageService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 留言反馈 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-09-20
 */
@RestController
@CrossOrigin
@RequestMapping("/back/message")
public class MessageController {
    @Autowired
    MessageService messageService;
    @Autowired
    LoanService loanService;
//仅查询没有贷款留言的企业
    @GetMapping("/getFirmMsg")
    public R getFirmMsg(){
    List<Firm> firms =messageService.getFirmMsg();
        return R.ok().data("FirmMsg",firms);
    }
    //查询所有已贷款企业的留言:只能查到有企业留言信息的贷款
    @GetMapping("/getAllMsg")
    public R getAllMsg(){
      //List<Message> message= messageService.getBaseMapper().selectList(new QueryWrapper<Message>().notIn("company_id",""));
//        List<MessageManage> message=messageService.getAllFirmMsg();
        List<LoanInfo> loanInfo=loanService.getAllLoanWithMsg();
        return R.ok().data("msg",loanInfo);
    }
//    根据贷款的id查询评论详情
    @GetMapping("/getFirmMsgDetail/{id}")
    public R getFirmMsgDetail(@PathVariable int id){
List<MessageManage> messageManages=messageService.getFirmMsgDetail(id);
        return R.ok().data("msgDetail",messageManages);
    }
//    根据企业id查询普通用户的留言
    @GetMapping("/getFirmMsgByfirmId/{firmId}")
    public R getFirmMsgByfirmId(@PathVariable int firmId){
        List<MessageManage> messageManages=messageService.getFirmMsgByfirmId(firmId);
        return R.ok().data("msgDetail",messageManages);
    }




//    回复操作

    /**
     *
     * @param magMsg  管理员回复内容
     * @param maid  管理员id
     * @param mid   要更新的留言id
     * @return
     */
    @GetMapping("/getManageReply/{maid}/{loanId}")
    public R ManageReply(@PathVariable int maid,@PathVariable int loanId,@RequestParam("magMsg") String magMsg){
        Message message=new Message();
        message.setApllyLoanId(loanId);
        message.setManageId(maid);
        message.setMagMsg(magMsg);
        messageService.getBaseMapper().insert(message);
        return R.ok();
    }

    @GetMapping("/ReplyMsg/{maid}/{firmId}")
    public R ReplyMsg(@PathVariable int maid,@PathVariable int firmId,@RequestParam("magMsg") String magMsg){
        Message message=new Message();
       message.setCompanyId(firmId);
        message.setManageId(maid);
        message.setMagMsg(magMsg);
        messageService.getBaseMapper().insert(message);
        return R.ok();
    }

//    业务员查看留言
    @GetMapping("findMsgByLoan/{loanId}")
    public R findMsgByLoan(@PathVariable int loanId){
        List<MessageManage> msgs=messageService.findMsgByLoan(loanId);
        return R.ok().data("msgs",msgs);
    }
//    业务员回复留言
    @PostMapping("/sandMsg/{manageId}/{reloanId}")
    public R sandMsg(@RequestParam String magMsg ,@PathVariable int manageId,@PathVariable int reloanId){
        Message message=new Message();
        message.setMagMsg(magMsg);
        message.setManageId(manageId);
        message.setApllyLoanId(reloanId);
        messageService.getBaseMapper().insert(message);
        return R.ok();
    }

    /////////////////////////////////张
    //   查询所有银行给业务员的留言回复
    @ApiOperation("获取留言信息")
    @GetMapping("/getMessageCobank/{bankId}")
    public R getMessageCobank(@PathVariable int bankId) {

        List<Message> messageList = messageService.selectAll(bankId);
        System.out.println("===银行===messageList======" + messageList);
        return R.ok().data("messageList", messageList).message("成功获取业务员回复");

    }


    //   根据ID获取当前银行下的所有业务员的回复和留言
    @ApiOperation("获取当前银行的留言信息")
    @GetMapping("/getMessageCobankByID/{mid}")
    public R getMessageCobankID(@PathVariable int mid) {

        Message messages = messageService.selectID(mid);
        System.out.println("===银行ID===messages======" + messages);
        return R.ok().data("messages", messages).message("成功获取银行回复");
    }


    //    留言回复插入
    @ApiOperation("获取当前银行的留言信息")
    @GetMapping("/insertMessage/{businessId}/{mid}")
    public R insertMessage(@RequestParam("reply") String reply ,
                           @PathVariable int businessId,@PathVariable int mid) {
        Message messages = new Message();
        messages.setInformation(reply);
        messages.setBusinessId(businessId);
        messages.setMid(mid);
        messageService.getBaseMapper().updateById(messages);
        System.out.println("===银行ID===messages======" + messages);
        return R.ok();
    }

}

